'use strict';
const db = uniCloud.database();

exports.main = async (event, context) => {
  const { fileContent, fileName, fileType } = event;
  
  if (!fileContent || !fileName) {
    return {
      code: 400,
      msg: '缺少文件内容或文件名'
    };
  }

  try {
    // 使用uniCloud.uploadFile上传图片
    const result = await uniCloud.uploadFile({
      cloudPath: `chat-images/${Date.now()}-${fileName}`,
      fileContent: fileContent
    });

    if (result.fileID) {
      return {
        code: 200,
        msg: '图片上传成功',
        data: {
          fileID: result.fileID,
          url: result.fileID
        }
      };
    } else {
      return {
        code: 500,
        msg: '图片上传失败'
      };
    }
  } catch (error) {
    console.error('图片上传失败:', error);
    return {
      code: 500,
      msg: '图片上传失败',
      error: error.message
    };
  }
};